﻿using System;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.Post;
using PMIS.BLL.User;

namespace PMIS.UI.User
{
	/// <summary>
	/// 职称信息维护
	/// </summary>
	public partial class 职称信息维护 : BaseForm
	{
		#region 私有变量
		/// <summary>
		/// 当前操作的记录
		/// </summary>
		private 职业_职称 CurrentRecord = null;
		#endregion

		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 职称信息维护 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 职称信息维护()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <param name="ID">记录ID</param>
		/// <param name="strUserID">工号</param>
		/// <returns></returns>
		public static 职称信息维护 GetInstance(string ID, string strUserID)
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 职称信息维护();
				}
			}

			if (ID == "new")
			{
				_form.CurrentRecord = new 职业_职称();
				_form.CurrentRecord.取得时间 = DateTime.Now;
				_form.CurrentRecord.USER_ID = strUserID;
			}
			else
			{
				_form.CurrentRecord = Singleton<BL_职称>.GetInstance().GetEntity(ID);
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void UserProfessionalTitleOperationForm_Load(object sender, EventArgs e)
		{
			UpdateFormView();
		}
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新界面显示
		/// </summary>
		private void UpdateFormView()
		{
			if (CurrentRecord == null)
				return;

			try
			{
				cbPostLevel.BeginUpdate();
				cbPostLevel.DataSource = Singleton<BL_岗位_分类_等级>.GetInstance().GetDetails("职称");
				cbPostLevel.EndUpdate();

				//	职称名称
				tbName.Text = CurrentRecord.职称名称;
				//	岗位等级
				cbPostLevel.Text = CurrentRecord.等级;
				//	取得时间
				dtpDate.Value = CurrentRecord.取得时间;
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 检查输入
		/// </summary>
		/// <returns></returns>
		private bool CheckInput()
		{
			if (string.IsNullOrEmpty(tbName.Text.Trim()))
			{
				ShowErrorMessage("职称名称不能为空!");
				tbName.Focus();
				return false;
			}

			if (string.IsNullOrEmpty(cbPostLevel.Text.Trim()))
			{
				ShowErrorMessage("职称级别不能为空!");
				cbPostLevel.Focus();
				return false;
			}

			return true;
		}
		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btSave_Click(object sender, EventArgs e)
		{
			if (!CheckInput())
				return;

			try
			{
				//	职称名称
				CurrentRecord.职称名称 = tbName.Text.Trim();
				//	职称级别(岗位级别)
				CurrentRecord.等级 = cbPostLevel.Text.Trim();
				//	取得时间
				CurrentRecord.取得时间 = dtpDate.Value;
				//	岗位
				CurrentRecord.岗位等级ID = (cbPostLevel.SelectedItem as 岗位_分类_等级).ID;

				//	保存
				if (Singleton<BL_职称>.GetInstance().Save(CurrentRecord))
				{
					ShowSuccessMessage("保存成功!");
					DialogResult = DialogResult.OK;
					this.Close();
				}
				else
				{
					ShowErrorMessage("保存记录时发生错误!");
					DialogResult = DialogResult.Cancel;
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 取消
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}
		#endregion
	}
}